package com.learning.mediacomponent;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSource;

public class VideoActivity extends AppCompatActivity {

    private ExoPlayer exoPlayer;
    private StyledPlayerView playerView;

    private ActivityResultLauncher<Intent> videoPickerLauncher;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        playerView = findViewById(R.id.pv_content);

        // Initialize ExoPlayer
        exoPlayer = new ExoPlayer.Builder(this).build();
        playerView.setPlayer(exoPlayer);

        Button btnVideoChoose = findViewById(R.id.btn_video_choose);
        Button btnVideoOnline = findViewById(R.id.btn_video_online);

        // 选择本地视频
        btnVideoChoose.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("video/*");
            videoPickerLauncher.launch(intent);
        });

        // 播放网络视频
        btnVideoOnline.setOnClickListener(v -> {
            Uri uri = Uri.parse("https://v6.huanqiucdn.cn/4394989evodtranscq1500012236/da4fee0f1397757896412331365/v.f100830.mp4");
            playVideo(uri);
        });

        // Register the activity result launcher
        videoPickerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == RESULT_OK) {
                Uri uri = result.getData().getData();
                if (uri != null) {
                    playVideo(uri);
                }
            }
        });
    }


    private void playVideo(Uri uri) {
        exoPlayer.clearMediaItems();
        DataSource.Factory factory = new DefaultDataSource.Factory(this);
        // 创建指定地址的媒体对象
        MediaItem videoItem = new MediaItem.Builder().setUri(uri).build();
        // 基于工厂对象何媒体对象创建媒体来源
        MediaSource videoSource = new ProgressiveMediaSource.Factory(factory).createMediaSource(videoItem);
        exoPlayer.setMediaSource(videoSource); //设置播放器的媒体来源
        exoPlayer.addListener(new Player.Listener() {
            @Override
            public void onPlaybackStateChanged(int playbackState) {
                if (playbackState == Player.STATE_BUFFERING) {
                } else if (playbackState == Player.STATE_READY) {
                } else if (playbackState == Player.STATE_ENDED){
                }
            }
        });
        exoPlayer.prepare();
        exoPlayer.play();
    }

    @Override
    protected void onStop() {
        super.onStop();
        exoPlayer.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        exoPlayer.release();
    }
}